package org.lixf.controllers;

import org.lixf.beans.Student;
import org.lixf.services.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class RegisterController {

    private IStudentService studentService;

    @Autowired
    public void setStudentService(IStudentService studentService) {
        this.studentService = studentService;
    }

    @RequestMapping("/register")
    public String register() {
        return "/register.jsp";
    }

    @RequestMapping("/doRegister")
    public void doRegister(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String num = req.getParameter("num");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String score = req.getParameter("score");

        Student student = new Student();
        student.setNum(num);
        student.setPassword(password);
        student.setName(name);
        student.setAge(Integer.parseInt(age));
        student.setScore(Double.parseDouble(score));

        studentService.register(student);

        resp.sendRedirect(req.getContextPath() + "/login");
    }
}
